/********************************************************************************
 * Copyright (c)2016-2020, ShenZhen WanJiaAn Technology CO.,LTD.
 * All Rights Reserved.
 *
 * File Name     : LogManager.c
 * Author        : joki.zhu
 * Date          : 2020-04-17
 *
 * Record        :
 * 1.Date        : 2020-04-17
 *   Author      : joki.zhu
 *   Modification: Created file
 ********************************************************************************/

#include "app_common.h" 
#include "LogManager.h"
#include "AvProto.h"
#include "System.h"
#include "log.h"
#include "DevConfig.h"
#include "Net.h"

static char gs_bLogInit = 0;

static void OnRecvLogEvent(unsigned int ev, void* data)
{
	switch (ev)
	{
	case _ukProtoRespond | __MsgConnect: 
        {
#if 0
            C_Connect* _Connect = (C_Connect*)(data);
            time_t ct = time(NULL);
            if (abs(ct - _Connect->TimeVal_TvSec) > 3) {
            //set system time;;;
            }
#endif
        }
        break;

    case _ukProtoGet | __MsgSystemVersion: 
        {
            C_SystemVersion* _SystemVersion = (C_SystemVersion*)(data);
            DevConfigGetSoftVersion(_SystemVersion->AppVersionString, sizeof(_SystemVersion->AppVersionString));
        }
        break;

    case _ukProtoGet | __MsgNetCommProfile: 
        {
            C_NetCommProfile* _NetCommProfile = (C_NetCommProfile*)(data);
            _NetCommProfile->NetGetType = NetCommT_Wireless;

            NET_ATTR_INFO_S stNetAttrInfo;
            
            memset(&stNetAttrInfo, 0, sizeof(stNetAttrInfo));
            NetGetAttrInfo("wlan0", &stNetAttrInfo);
            sprintf(_NetCommProfile->IpAddr, "%s", stNetAttrInfo.ip);
            sprintf(_NetCommProfile->SubMask, "%s", stNetAttrInfo.netmask);
            sprintf(_NetCommProfile->GateWay, "%s", stNetAttrInfo.gateway);
            sprintf(_NetCommProfile->Mac, "%s", stNetAttrInfo.mac);
            sprintf(_NetCommProfile->DnsI, "%s", stNetAttrInfo.dns1);
            sprintf(_NetCommProfile->DnsII, "%s", stNetAttrInfo.dns2);
        }
        break;

    default:
        break;
	}

}


int LogInit(void)
{
#ifdef LOG_PUSH_ENABLE
    int ret = 0;
    char uuid[32] = "";
    DevConfigGetUuid(uuid, sizeof(uuid));
    ret = AvProtoServiceStart(NULL, uuid, OnRecvLogEvent);
    if (ret < 0) {
        LOGE("AvProtoServiceStart failed\n");
        return -1;
    }
    gs_bLogInit = 1;
#endif /* LOG_PUSH_ENABLE */

    return 0;
}


int LogPushData(HI_LOG_LEVEL_E enLogLevel, char *fileName, int line, char *pData)
{
#ifdef LOG_PUSH_ENABLE
    if (gs_bLogInit == 0) {
        _logDebugErr("LogInit dont't init!\n");
        return -1;
    }
    
    LogItem _iteam;
    struct timeval tv;
    
    //AppRunMSec(_iteam.UpTime);
    
    AvGetTimeOfDay(&tv, NULL);
    _iteam.FileLine = line;
    snprintf(_iteam.FileName, sizeof(_iteam.FileName), "%s", fileName);
    _iteam.Timeval_tvSec = tv.tv_sec;
    _iteam.Timeval_tvUsec = tv.tv_usec;

    switch (enLogLevel)
    {
        case HI_LOG_LEVEL_FATAL:
            _iteam.Type = TLogHint;
            break;
        case HI_LOG_LEVEL_ERROR:
            _iteam.Type = TLogError;
            break;
        case HI_LOG_LEVEL_WARNING:
            _iteam.Type = TLogWarn;
            break;
        case HI_LOG_LEVEL_INFO:
            _iteam.Type = TLogMsg;
            break;
        case HI_LOG_LEVEL_DEBUG:
            _iteam.Type = TLogMsg;
            break;
    }
    
    snprintf(_iteam.Msg, sizeof(_iteam.Msg), "%s", pData);
    
    AvProtoPushDevLog(&_iteam);
    
#endif /* LOG_PUSH_ENABLE */

    return 0;
}

int LogDeint(void)
{
    
}

